package poker.preflopsim;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.NumberFormat;
import poker.Card;

public class PreFlopSimulator {

	GameRoundNoGUI gameRoundNoGUI;

	static final int ROWS = 169; //169 + (1 rad for � si antall spillere k)
	static final int COLS = 9; // 9 + (1 kolonne til � ha regler for kortverdi og suit/offsuit

	double[][] statistics;
	String[] prefixTable;
	Card[][] equivalenceClasses;

	/*
	 * GameRoundNoGUI. 
	 * 	- k Players
	 *  - deal pocketCards
	 *  - deal board (flop, turn, river)
	 *  - compareHands
	 */

	public PreFlopSimulator(int numberOfRounds)
	{		
		statistics = new double[ROWS][COLS];
		prefixTable = new String[ROWS];
		equivalenceClasses = new Card[ROWS][2];

		fillPrefixTable();
		fillEquivalenceClasses();
		simulate(numberOfRounds);
	}
	
	private void simulate(int numberOfRounds)
	{
		for(int eqClass = 0; eqClass < equivalenceClasses.length; eqClass++)
		{
			Card[] startCards = equivalenceClasses[eqClass];
			printStartCards(startCards);
			for(int players = 2; players < 11; players++)
			{
				System.out.println("Players: " + players);
				gameRoundNoGUI = new GameRoundNoGUI(players, numberOfRounds, startCards);
				gameRoundNoGUI.start();
				statistics[eqClass][players - 2] = gameRoundNoGUI.getPlayerStats().winPercentage();
			}
		}
	}
	
	public static void printStartCards(Card[] cards)
	{
		for (int i = 0; i < cards.length; i++) {
			System.out.print(cards[i] + ", ");
		}
		System.out.println();
	}

	public PreFlopSimulator(){
		prefixTable = new String[ROWS];		
		fillPrefixTable();
	}

	private void fillEquivalenceClasses()
	{
		int listIndex = 0;
		for (int x = 2; x < 15; x++) {
			for (int z = x; z < 15; z++) {

				if(x == z)
				{ 
					equivalenceClasses[listIndex][0] = new Card('c', x);
					equivalenceClasses[listIndex][1] = new Card('h', z); 
					listIndex++;
				}

				else{
					equivalenceClasses[listIndex][0] = new Card('d', x);
					equivalenceClasses[listIndex][1] = new Card('d', z); 
					listIndex++;
					equivalenceClasses[listIndex][0] = new Card('c', x);
					equivalenceClasses[listIndex][1] = new Card('h', z); 
					listIndex++;
				}
			}
		}
	}
	
	public void printEquivalenceClasses()
	{
		for(int i = 0; i < equivalenceClasses.length; i++)
		{
			System.out.println(equivalenceClasses[i][0] + " " + equivalenceClasses[i][1]);
		}
	}

	public void fillPrefixTable()
	{
		int listIndex = 0;
		for (int x = 2; x < 15; x++) 
		{
			for (int z = x; z < 15; z++) 
			{
				if(x == z)
				{ 
					prefixTable[listIndex] = x + "" + z + "o"; 
					listIndex++;
				}

				else
				{
					prefixTable[listIndex] = x + "" + z + "s";
					listIndex++;
					prefixTable[listIndex] = x + "" + z + "o";
					listIndex++;
				}
			}
		}
	}

	public String[] getPrefixTable(){ return prefixTable; }
	
	public void saveStatistics(String fileName)
	{
		PrintWriter writer = null;
		try
		{
			File file = new File(fileName);
			writer = new PrintWriter(new BufferedWriter(new FileWriter(file)));
			
			for (int x = 0; x < ROWS; x++) {
				writer.write(prefixTable[x] + ": ");
				for (int y = 0; y < COLS; y++) {
					NumberFormat nf = NumberFormat.getInstance();
					nf.setMaximumFractionDigits(5);
					writer.write(statistics[x][y] + " ");
				}
				writer.write("\n");
			}
		} 
		catch(IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(writer != null)
				writer.close();
		}
	}	
}
